<template>
  <div  class="card">
    <div class="card" style="margin-bottom: 5px">
      <el-input v-model="data.name" placeholder="请输入名称查询" style="width: 300px; margin-right: 10px;"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
      <el-button type="success" @click="handleAdd">新增</el-button>
      <el-button type="danger" @click="delBatch">批量删除</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px;">
      <el-table :data="data.tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" />
        <el-table-column fixed="right" label="操作" min-width="160">
          <template #default="scope">
            <el-button type="primary" round plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" round plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--对话窗口-->
    <el-dialog v-model="data.formVisible" title="分类" width="40%">
      <el-form :model="data.form" label-width="80px" style="padding-right: 20px">
        <el-form-item label="名称">
          <el-input v-model="data.form.name" autocomplete="off" placeholder="请输入分类名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
  tableData: [],
  total: 0,
  pageNum: 1,
  pageSize: 20,
  name: null,
  formVisible: false,
  form: {}
})

const load = () => {
  request.get('/category/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name
    }
  }).then(res => {
    console.log(res.data.list)
    data.tableData = res.data.list
    data.total = res.data.total
  })
}
load()

const reset = () => {
  data.name = null
  load()
}

// 弹出新增窗口
const handleAdd = () => {
  data.form = {}
  data.formVisible = true
}

// 新增
const add = () =>{
  if(!data.form.name){
    ElMessage.error('请输入分类名称')
  }else{
    request.post('/category/add', data.form).then((res) => {
      if(res.code === '200'){
        load()
        data.formVisible = false
        ElMessage.success('操作成功')
      }else{
        ElMessage.error(res.msg)
      }
    })
  }
}

// 弹出编辑窗口
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

// 编辑
const edit = () =>{
  if(!data.form.name){
    ElMessage.error('编辑后内容不能为空')
  }else{
    request.put('/category/update', data.form).then(res => {
      if(res.code === '200'){
        load()
        data.formVisible = false
        ElMessage.success('操作成功')
      }else{
        ElMessage.error(res.msg)
      }
    })
  }
}

// 保存新增/编辑信息
const save = () => {
  data.form.id ? edit() : add()
}

// 删除
const del = (id) =>{
  ElMessageBox.confirm('删除后无法恢复，您确定删除吗？', '确认删除', {type: "warning"}).then(res => {
    request.delete('/category/delete/'+id).then(res => {
      if(res.code === '200'){
        load()
        ElMessage.success('操作成功')
      }else{
        ElMessage.error(res.msg)
      }
    })
  })
}

/**
 * 批量删除
 * @param rows
 */
const handleSelectionChange = (rows) => {
  data.ids = rows.map(row => row.id)
}
const delBatch = () => {
  if(!data.ids){
    ElMessage.error('请选择要删除的数据')
  }else{
    ElMessageBox.confirm('删除后无法恢复，确定要删除吗？', '确认删除', {type: "warning"}).then(res => {
      request.delete('/category/delete-batch' , {data:data.ids}).then(res => {
        if (res.code === '200') {
          load()
          ElMessage.success('操作成功')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch(err => {
    })
  }
}
</script>